package py.com.alfa.escriba.gui.controller.view;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;

public class ViewManager {

	private Logger logger = LoggerFactory.getLogger(getClass());
	private Map<String, ViewContext> contexts;
	private Permissions permissions;
	private HashMap<String, String> permissionsMap;
	
	public ViewManager() {
		setContexts(new HashMap<String, ViewContext>());
	}
	
	public void mapPermissions(Set<String> permissions){
		if(permissionsMap != null && !permissionsMap.isEmpty()){
			for(String p:permissions){
				if(permissionsMap.containsKey(p)){
					String fieldName = permissionsMap.get(p);
					logger.info("field name : {}", fieldName);
					Field field = ReflectionUtils.findField(Permissions.class, fieldName);
					logger.info("method: {}", field);
					if(field != null){
						ReflectionUtils.makeAccessible(field);
						ReflectionUtils.setField(field, getPermissions(), true);
					}
				}
			}
		}
	}
	
	public void registerContext(String key, ViewContext context){
		if(getContexts() != null && !getContexts().containsKey(key)){
			getContexts().put(key, context);
		}
	}
	
	public ViewContext getContext(String key){
		if(getContexts() != null && getContexts().containsKey(key)){
			return getContexts().get(key);
		}
		return null;
	}
	
	public void printPermissions(){
		logger.info("{}", getPermissions());
	}

	public Map<String, ViewContext> getContexts() {
		return contexts;
	}

	public void setContexts(Map<String, ViewContext> contexts) {
		this.contexts = contexts;
	}

	public Permissions getPermissions() {
		return permissions;
	}
	public void setPermissions(Permissions permissions) {
		this.permissions = permissions;
	}
	public HashMap<String, String> getPermissionsMap() {
		return permissionsMap;
	}
	public void setPermissionsMap(HashMap<String, String> permissionsMap) {
		this.permissionsMap = permissionsMap;
	}


	
}
